﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DaoYou.Web.Guider.Account;
using RenChangYou.Common;

namespace DaoYou.Web.Account
{
    public partial class UserGuiderInfo : PageBase
    {
        protected string GuideNoImg { get; set; }
        protected string IdCardImg { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RenChangYou.BLL.DY_GuiderInfo business = new RenChangYou.BLL.DY_GuiderInfo();
                RenChangYou.Model.DY_GuiderInfo guider = business.GetModelByCache(base.Account);

                if (guider != null)
                {
                    this.txt_GuideNo.Value = guider.GuideNo;
                    this.txt_IdcardNo.Value = guider.IdCardNo;
                    this.sel_isEnglist.SelectedValue = guider.IsEnglishService.ToString();
                    this.sel_goabroad.SelectedValue = guider.IsGoAbroadService.ToString();
                    this.GuideNoImg = guider.GuideNoImageName;
                    this.IdCardImg = guider.IdCardImageName;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                #region 保存 DY_GuiderInfo
                RenChangYou.BLL.DY_GuiderInfo guiderbll = new RenChangYou.BLL.DY_GuiderInfo();
                RenChangYou.Model.DY_GuiderInfo guiderInfo = guiderbll.GetModelByCache(base.Account);

                if (guiderInfo == null)
                {
                    string s1 = UploadGuideNoImg();
                    string s2 = UploadIdCardNoImg();

                    guiderInfo = new RenChangYou.Model.DY_GuiderInfo()
                    {
                        GuideNo = txt_GuideNo.Value.Trim(),
                        GuideNoImageName = s1,
                        IdCardImageName = s2,
                        IdCardNo = txt_IdcardNo.Value.Trim(),
                        IsEnglishService = Convert.ToBoolean(sel_isEnglist.SelectedValue),
                        IsGoAbroadService = Convert.ToBoolean(sel_goabroad.SelectedValue),
                        UserName = base.Account,
                        VisitCount = 1
                    };

                    guiderbll.Add(guiderInfo);
                }
                else
                {
                    guiderInfo.UserName = Account;
                    guiderInfo.GuideNo = txt_GuideNo.Value.Trim();

                    if (this.fup_GuideNoImg.HasFile)
                    {
                        string s1 = UploadGuideNoImg();
                        guiderInfo.GuideNoImageName = s1;
                    }

                    guiderInfo.IsEnglishService = Convert.ToBoolean(sel_isEnglist.SelectedValue);
                    guiderInfo.IsGoAbroadService = Convert.ToBoolean(sel_goabroad.SelectedValue);
                    guiderInfo.IdCardNo = txt_IdcardNo.Value.Trim();

                    if (this.fup_IdCardNo.HasFile)
                    {
                        string s2 = UploadIdCardNoImg();
                        guiderInfo.IdCardImageName = s2;
                    }
                    guiderInfo.VisitCount += 1;

                    guiderbll.Update(guiderInfo);
                }
                #endregion

                Common.AlertMessageAndRedirect("提交成功", Page.ResolveUrl("~/Account/UserGuiderInfo.aspx"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private string UploadGuideNoImg()
        {
            bool fileOK = false;
            string path = HttpContext.Current.Server.MapPath(string.Format("~/upload/{0}/", base.Account));
            if (fup_GuideNoImg.HasFile)
            {
                String fileExtension = System.IO.Path.GetExtension(fup_GuideNoImg.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }
            }
            if (fileOK)
            {
                try
                {
                    if (!System.IO.Directory.Exists(path))//判断文件夹是否已经存在
                    {
                        System.IO.Directory.CreateDirectory(path);//创建文件夹
                    }

                    string ext = fup_GuideNoImg.FileName.Substring(fup_GuideNoImg.FileName.LastIndexOf(".") + 1, fup_GuideNoImg.FileName.Length - fup_GuideNoImg.FileName.LastIndexOf(".") - 1);
                    string newfilename = DateTime.Now.Ticks.ToString() + "." + ext;
                    fup_GuideNoImg.SaveAs(path + newfilename);

                    return newfilename;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return string.Empty;
        }

        private string UploadIdCardNoImg()
        {
            bool fileOK = false;
            string path = HttpContext.Current.Server.MapPath(string.Format("~/upload/{0}/", base.Account));
            if (fup_IdCardNo.HasFile)
            {
                String fileExtension = System.IO.Path.GetExtension(fup_GuideNoImg.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }
            }
            if (fileOK)
            {
                try
                {
                    if (!System.IO.Directory.Exists(path))//判断文件夹是否已经存在
                    {
                        System.IO.Directory.CreateDirectory(path);//创建文件夹
                    }
                    string ext = fup_IdCardNo.FileName.Substring(fup_IdCardNo.FileName.LastIndexOf(".") + 1, fup_IdCardNo.FileName.Length - fup_IdCardNo.FileName.LastIndexOf(".") - 1);
                    string newfilename = DateTime.Now.Ticks.ToString() + "." + ext;
                    fup_IdCardNo.SaveAs(path + newfilename);

                    return newfilename;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            return string.Empty;
        }
    }
}